<?php
include_once("../module/checkA.php");
include_once("../../config.php");
header("Content-type: text/html; charset=GBK");
if(!$_REQUEST ['applydate'])
{
    $_REQUEST ['applydate']=date("Y-m");
}
$applydate  = addslashes($_REQUEST ['applydate']);



//同意加上and，然后去除左边的and就可以了。
if($_REQUEST['select_keywords'])
{
    $keywords=" and (name like '%".$_REQUEST['select_keywords']."%' or remark like '%".$_REQUEST['select_keywords']."%' )";
    $keywords_url="&select_keywords=".$_REQUEST['select_keywords'];
}
else
{
    $keywords="";
    $keywords_url="";
}



if(($_REQUEST['select_type'])&&($_REQUEST['select_type']!="全部"))
{
    $type=" and classify = '".$_REQUEST['select_type']."'";
    $type_url="&select_type=".$_REQUEST['select_type'];
}
else
{
    $type="";
    $type_url="";
}






if(($_REQUEST['select_departmentid'])&&($_REQUEST['select_departmentid']!="全部"))
{
    $departmentid=" and departmentid = '".$_REQUEST['select_departmentid']."'";
    $departmentid_url="&select_departmentid=".$_REQUEST['select_departmentid'];
}
else
{
    $departmentid="";
    $departmentid_url="";
}










if($_REQUEST['select_startday'])
{
    $start_date=$_REQUEST['select_startday']." 00:00:00";
    $start_date_str=" and applydate > '".$start_date."'";
    $start_date_url="&select_startday=".$_REQUEST['select_startday'];
}
else
{
    $start_date_str="";
    $start_date_url="";
}




if($_REQUEST['select_endday'])
{
    $end_date=$_REQUEST['select_endday']." 23:59:59";
    $end_date_str=" and applydate < '".$end_date."'";
    $end_date_url="&select_endday=".$_REQUEST['select_endday'];
}
else
{
    $end_date_str="";
    $end_date_url="";
}







if($sortorder)
{
    $sortorder="order by ".$sortorder;
}
else
{
    $sortorder="";
}








$select_str=$keywords.$type.$departmentid.$start_date_str.$end_date_str;


//获取部门的 nid名字对应表
$department_full_list=select_lines_all_items($table_Prefix."department","","","");
$department_list=transform_2darray_in_1darray_with_new_key($department_full_list,"nid","name");
//获取员工的 nid名字对应表
$employee_full_list=select_lines_all_items($table_Prefix."employee","","","");
$employee_list=transform_2darray_in_1darray_with_new_key($employee_full_list,"nid","name");

?>
<?php
// 输出Excel文件头，可把user.csv换成你要的文件名
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="财务信息.xls"');
header('Cache-Control: max-age=0');

$stmt = select_lines_items($table_Prefix."finance","nid asc","name,cost,principal,departmentid,authur,classify,applydate,status,remark","(rid is null or rid = '0')".$select_str,"");







 $head = array('内容', '金额','用款人', '部门', '录入', '类型', '日期','状态','备注');


for($i=0;$i<count($head);$i++)

{
    $strings=$head[$i]."\t";

    if($i+1==count($head))
    {
        $strings .= "\n";
    }
    echo $strings;
}




for($i=0;$i<count($stmt);$i++)

{

    for($j=0;$j<count($head);$j++)

    {


         if($j==3)
        {
            $string .=  $department_list[$stmt[$i][$j]]."\t";
        }

        elseif(($j==2)or($j==4))
        {
            $string .=  $employee_list[$stmt[$i][$j]]."\t";
        }

        else
        {
        $string .=   $stmt[$i][$j]."\t";
        }
    }

    $string .= "\n";


}
echo iconv("UTF-8","GBK",$string);
//echo $string;

?>